package md.NJavaOppo;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import com.heytap.msp.mobad.api.ad.InterstitialAd;
import com.heytap.msp.mobad.api.listener.IInterstitialAdListener;

import md.NJavaBase.AdType;
import md.NJavaBase.IAd;
import md.NJavaBase.IAdCallback;
import md.NJavaBase.ICallbackLight;
import md.NJavaBase.NJavaBase;

//插屏广告实现
public class OppoInterstitialAD implements IAd{
    static String Tag = "OppoInterstitialAD";

    //广告类型
    public AdType getAdType(){
        return AdType.interstitial;
    }


    public OppoInterstitialAD(int priority)
    {
        m_Priority = priority;

        Activity activity = NJavaBase.getActivity();

        try {
            ApplicationInfo appInfo = activity.getPackageManager().getApplicationInfo(
                    activity.getPackageName(), PackageManager.GET_META_DATA
            );
 
            mInterstitialid = appInfo.metaData.getString("OPPO_INTERSTITIAL_ID") ;
            if(mInterstitialid.length()>0 && mInterstitialid.charAt(0)=='#')
                mInterstitialid = mInterstitialid.substring(1);

        } catch (PackageManager.NameNotFoundException e) {
            Log.e(Tag, "init error!");
            e.printStackTrace();
        }
    }

    public boolean needCheckLoadOvertime(){return false;}

    //是否已经初始化完毕，可以开始加载视频
    public boolean isInitialized(){return true;}

    //广告平台的名称
    public String adName() { return "OppoInterstitialAD"; }

    //当前视频是否已经过期
    public boolean isInvalidated(){return !mIsReady;}

    //预装视频 回调0表示预装成功 99表示预装失败  2视频播放结束可以获得奖励 3视频已关闭
    public void reloadAd(IAdCallback callBack)
    {
        if(m_Loading)
        {
            Log.d(Tag,"正在装在，忽略reloadAd动作");
            return;
        }

        if(mAd==null)//尚未装载成功
        {
            Log.d(Tag,"发起装载请求");
            m_Callback = callBack;

            this._load();//立即发起装载
        }
        else//已经是装载成功的
        {
            Log.d(Tag,"已经是装载成功的");
            callBack.onLoad();
                    //reCall(new Integer(0));
        }
    }

    void _load()
    {
        m_Loading = true;

        mAd= new InterstitialAd(NJavaBase.getActivity(), mInterstitialid);
        mAd.setAdListener( new IInterstitialAdListener(){
            //广告准备就绪
            @Override
            public void onAdReady(){
                m_Loading = false;
                mIsReady = true;
                m_Callback.onLoad();//reCall(new Integer( 0));
            }

            //广告关闭
            @Override
            public void onAdClose()
            {
                Log.i(Tag, "onInterstitialAdClose");
                m_Callback.onClose();//reCall(new Integer(3)); //通知关闭
            }

            @Override
            public void onAdShow()
            {

            }

            @Override
            public void onAdFailed(String message)
            {
                Log.d(Tag, "onAdFailed message:" + message);
                m_Loading = false;
                m_Callback.onError(message);//reCall(new Integer(99));
            }

            @Override
            public void onAdFailed(int code, String message)
            {
                Log.d(Tag, "onAdFailed: code:" + code + ",message:" + message);
                m_Loading = false;
                m_Callback.onError(message);//reCall(new Integer(99));
            }

            @Override
            public void onAdClick()
            {

            }
        });
        mAd.loadAd();
    }


    //显示视频
    public void show()
    {
        if(!isInvalidated())//调用show之前，外部已经判断过是否装成功，如果这里返回false，应该是有问题
        {
            mIsReady = false;
            mAd.showAd();
        }
        else
            _load();//立即发起装载
    }

    //优先级，值越大越优先
    public int getPriority(){return m_Priority;}

    final int m_Priority;

    private InterstitialAd mAd;
    boolean m_Loading = false;
    boolean mIsReady = false;
    IAdCallback m_Callback;
    String mInterstitialid = "";
}